package storm.DPMS;
import java.io.*;
import java.util.Iterator;
import java.util.Vector;

import storm.Processors.Processor;
import storm.Tasks.Task;
class cpuInfo{
	int duree_temps_creux;
	int nombre_temps_creux;
	
}
public class observer_test1 extends DPM {
	
	public Vector info;
	public Vector<cpuInfo> cpuInfos;
	public int preemption=0;
	private int M=0;
	public int taskmigration=0;
	public int jobmigration=0;
	public int total_migrations=0;
	public int job_rel=0;
	public int resdiff=0; 
	public int release_s=0;
	public int release_d=0;
	public int job_completion=0;
	private int prev=0;       // store the ID of previous processor
	private int P_Id=0;
	private int present=0;
	private int N=0;
	private Task T;
	private int duration=0;
	private String filename; 
	public  static final int num_Sched=3;
	public static int linebreak=0;
	
	
	  public void init(){
				
			 info = new Vector();
			 cpuInfos = new Vector<cpuInfo>();
			 Iterator I =  this.Kernel.getTasksListeManager().getALLTask().iterator();
	         M=this.Kernel.getProcessorTable().size();
			 N =this.Kernel.getTasksListeManager().getALLTask().size(); 
			 duration=this.Kernel.getDuration();
			// Set the values for the first time
			 while (I.hasNext()){
		     	 Task crt = (Task)I.next();
		     	 crt.setOwnFieldIntValue("prestate",0);
		       	 crt.setOwnFieldIntValue("first_running",0);
		    	     crt.setOwnFieldIntValue("Lastproc",-1);
	     	  }	
			  
			 //filename= "N"+N+"M"+M+".text";
		//	 filename=this.getOwnFieldStringValue("output");
	   }
	
	
	  public void onTick(){  // AT each tick 
		    
			String res ="";
			int  date = this.Kernel.getTime();
		    Iterator I =  this.Kernel.getTasksListeManager().getALLTask().iterator();
		   	while(I.hasNext()){
				Task crt = (Task) I.next();
	//		    			preemption=preemption+1;
//			    			System.out.println("Preempt " + crt.getName() + " --" + this.Kernel.getTime() + "-- " + crt.getProcessor());
			 
			    if(crt.isIsrunning()){  // THE CASE WHEN TASK IS RUNNING
	 		    	    Processor P = (Processor)crt.getProcessor();
	 		    	 	int pred = crt.getOwnFieldIntValue("Lastproc");
	 		    	 	int first =crt.getOwnFieldIntValue("first_running");
	 		    	 	if (pred != P.getId()) {
	 		    	 			crt.setOwnFieldIntValue("Lastproc",P.getId());
	 		    	 			if (first > 0 ) {
	 		    	 				total_migrations = total_migrations +1 ;
	 		    	 			}
	 		    	 			else {
	 		    	 				crt.setOwnFieldIntValue("first_running",1);
	 		    	 			}
	 		    	 		}
				    	crt.setOwnFieldIntValue("prestate",2);
	 		    	 }
	    	 		//System.out.println(crt.getName() +"--" +this.Kernel.getTime()+"--"+crt.getState()+"--"+crt.getEET()+"--"+crt.getRET());

			    if (crt.isready) {
 		    	 		int prestate =crt.getOwnFieldIntValue("prestate");
 		    	 	//	System.out.println(crt.getName() +"--" +this.Kernel.getTime()+"--"+prestate+"--"+crt.getEET()+"--"+crt.getRET());
 		    	 		if ((prestate == 2 ) && (crt.isEverRunning() )){
 		    	 			preemption=preemption+1;
 		    	 		//	System.out.println("Preempt " + crt.getName() + " --" + this.Kernel.getTime() + "-- " + crt.getProcessor());
 	 		    	 		crt.setOwnFieldIntValue("prestate",0);

 		    	 		}

 		    	 	}

			  }
         
			  //END WHILE
		   					    
				 /*crt.setOwnFieldIntValue("Previous",present);
				 info.add(crt);
				 res = res + crt.toString() + "\n";
			  }*/
		  // 	total_migrations= taskmigration+jobmigration;
 		    	   
 		if (date==duration)
   		{
 			System.out.println("Total migration "+ total_migrations+"  Preemptions" +preemption);
  
   		
  
				
					
		}
 		//CPU TEMPS CREUX 
 		Iterator c = this.Kernel.getTasksListeManager().getALLProcessors().iterator();
 		
 		
 		
				  
	}
	
}
